from utils.base_util import DBConnection
from utils.ui_util import UIAct


class TestCase:
    '''
    自定义TestCase
    '''

    def setup(self):
        print('这是测试的准备工作')

    def teardown(self):
        print('这是测试的收尾工作')

    def assert_equal(self, data, actual):  # data包含了测试用例的信息，包含了期望结果
        '''
        断言相等
        '''
        case_id = 0
        params = 0
        expected = 0
        type = 0
        step = 0
        version_id = 0
        module_name = 0

        if "case_no" in data:
            case_id = data["case_no"]
        if "params" in data:
            params = data["params"]
        if "expected" in data:
            expected = data["expected"]
        if "test_type" in data:
            type = data["test_type"]
        if "test_steps" in data:
            step = data["test_steps"]
        if "version" in data:
            version_id = data["version"]
        if "module" in data:
            module_name = data["module"]
        test_result = ""
        img_path = ""
        err_msg = ""

        if expected == actual:
            test_result = "pass"
        else:
            test_result = "fail"
            err_msg = "assert fail"
            if type == "UI":
                img_path = UIAct.screenshot(version_id)

        # 将以上信息写入数据库对应的测试结果表中
        sql = f'insert into sharedParkingTestResult(version_id, module_name,case_id,' \
              f'`type`,step,test_result,err_msg,screenshot_path) ' \
              f'values("{version_id}","{module_name}","{case_id}","{type}",' \
              f'"{step}","{test_result}","{err_msg}","{img_path}")'

        DBConnection.dml(sql, 'test_db')

if __name__ == '__main__':
    data = {"case_no": "13030120075", "params": "{'key':'msra'}", "expected": "success",
                "test_type": "api", "test_steps": "1.2.3.4.5.", "version": "4.0", "module": "admint"}
    tc = TestCase()
    tc.assert_equal(data,"fail")

